﻿using System;
using System.Net;
using MingleReport.HttpRequests;

namespace MingleReport.Services
{
    public class LoginService : ILoginService
    {
        private readonly string serverUrl;
        private readonly IHttpRequestFactory httpRequestFactory;
        private MingleHttpAuthenticationRequest request;

        public LoginService(string serverUrl, IHttpRequestFactory httpRequestFactory)
        {
            this.serverUrl = serverUrl;
            this.httpRequestFactory = httpRequestFactory;
        }

        public bool Login(string username, string password)
        {
            try
            {
                request = httpRequestFactory.GetAuthenticationRequest(serverUrl, username, password);
                request.Create();
                request.GetResponse();
            }
            catch (Exception e)
            {
                // TODO: how to determine a failed login attemp ? 
                Console.WriteLine(e);
                return false;
            }
            return true;
        }

        public CookieContainer Cookie
        {
            get { return request.CookieContainer; }
        }
    }
}